package ATM.logic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import ATM.data.Accounts;
import ATM.data.AtmData;
import ATM.data.CustomData;
import ATM.gui.Screen;

public class AtmActionListener implements ActionListener {

	Screen screen;
	AcLMethods acLMethods;

	StringBuilder usb = new StringBuilder();
	StringBuilder psb = new StringBuilder();
	private int counter = 0;
	String un;

	public AtmActionListener(Screen screen) {
		this.screen = screen;
		this.acLMethods = new AcLMethods(screen);

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String ac = arg0.getActionCommand();
		acLMethods.Numpad(ac);
		CustomData customData = new CustomData(screen);
		un = screen.getWelcome().getUserField().getText();
		ArrayList<Accounts> accounts = customData.getCustomersAccounts(Integer
				.parseInt(un));

		if (ac.equals("WelcomeEnter")) {
			acLMethods.login();
		} else if (ac.equals("Clear")) {
			acLMethods.Exit();
		}

		else if (ac.equals("ViewBalance")) {
			screen.show("VIEWBALANCE");
			acLMethods.ViewCurrentBalance(accounts);
		}

		else if (ac.equals("WtihdrawCash")) {
			acLMethods.Withdraw();
		}

		else if (ac.equals("Deposit")) {
			screen.show("DEPOSIT");
		}

		else if (ac.equals("Exit")) {
			acLMethods.Exit();
		}

		else if (ac.equals("NextVB")) {
			if (counter + 1 < accounts.size()) {
				counter++;
				ViewCurrentBalance(accounts);
			} else {
				counter = 0;
				ViewCurrentBalance(accounts);
			}

		}

		else if (ac.equals("PreviousVB")) {
			if (counter > 0) {
				counter--;
				ViewCurrentBalance(accounts);
			} else {
				counter = accounts.size() - 1;
				ViewCurrentBalance(accounts);
			}
		}

		else if (ac.equals("ExitViewBal")) {
			acLMethods.Exit();
			counter = 0;
		}

		// withdraw cash
		else if (ac.equals("Withdraw100")) {
			acLMethods.WithdrawAmount(100);
		} else if (ac.equals("Withdraw200")) {
			acLMethods.WithdrawAmount(200);
		} else if (ac.equals("Withdraw500")) {
			acLMethods.WithdrawAmount(500);
		} else if (ac.equals("Withdraw1000")) {
			acLMethods.WithdrawAmount(1000);
		}

		if (ac.equals("DepositFunds")) {
			acLMethods.Deposit();
		}
	}

	public void ViewCurrentBalance(ArrayList<Accounts> accounts) {
		String accType = accounts.get(counter).getType();
		String allaccounts = Integer.toString(accounts.size());
		String avBal = Double.toString(accounts.get(counter)
				.getAvailableBalance());
		String toBal = Double.toString(accounts.get(counter).getTotalBalance());
		String rate = Double.toString(accounts.get(counter).getInterestRate());
		String ovDraft = Double.toString(accounts.get(counter).getOverdraft());
		screen.getViewBalance().setTextField(accType, avBal, toBal, rate,
				ovDraft, Integer.toString(counter + 1), allaccounts);

	}
}